package com.yydm.servicecount.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yydm.commonutils.R;
import com.yydm.servicecount.client.UcenterClient;
import com.yydm.servicecount.entity.StatisticsDaily;
import com.yydm.servicecount.mapper.StatisticsDailyMapper;
import com.yydm.servicecount.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yydm
 * @since 2022-07-16
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {

//        删除表中相同时间的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_statistics", day);
        baseMapper.delete(wrapper);

//        远程调用得到某一天注册人数
        R registerR = ucenterClient.registerCount(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");

//        把获取到的数据添加到数据库,统计分析表中
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(countRegister);
        statisticsDaily.setDateStatistics(day);

        statisticsDaily.setLoginNum(RandomUtils.nextInt(10, 60));
        statisticsDaily.setAutomobileNum(RandomUtils.nextInt(20, 30));
        baseMapper.insert(statisticsDaily);

    }

    //    获取图表数据
    @Override
    public Map<String, Object> getChartsData(String type, String begin, String end) {

        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
//        指定查询的字段
        wrapper.select(type, "date_statistics");
        wrapper.between("date_statistics", begin, end);

        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper);

        Map<String, Object> map = new HashMap<>();
        List<String> dateList = new ArrayList<String>();
        List<Integer> dataList = new ArrayList<Integer>();
        map.put("dateList", dateList);
        map.put("dataList", dataList);

        for (int i = 0; i < statisticsDailies.size(); i++) {
            StatisticsDaily statisticsDaily = statisticsDailies.get(i);
            dateList.add(statisticsDaily.getDateStatistics());

            switch (type) {
                case "register_num":
                    dataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(statisticsDaily.getLoginNum());
                    break;
                case "automobile_num":
                    dataList.add(statisticsDaily.getAutomobileNum());
                    break;
                default:
                    break;
            }
        }
        return map;
    }
}
